package com.dst.smart.handler.disposer;

import android.content.Context;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.player.entity.SongPlayInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * 食谱
 * 茄子怎么烧
 	干锅土豆
 	尖椒炒鸡蛋怎么做
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class CookBookDisposer extends Disposer {
	//private static final String KEY_URL = "url";

	public CookBookDisposer(Context context) {
		super(context);
	}
	
	@Override
	public void disposeResult(final SemanticResult semanticResult) {
		try {
			if (semanticResult != null) {
				JSONArray result = semanticResult.getResult();
				if (result != null && result.length() > 0) {
					List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
					SongPlayInfo songPlayInfo;
					if (semanticResult.getAnswerText().length() > 0) {
						//先播放標題
						songPlayInfo = new SongPlayInfo();
						songPlayInfo.setPlayUrl(semanticResult.getAnswerText());
						playList.add(songPlayInfo);
					}
					for (int index = 0; index < result.length(); index++) {
						JSONObject newsJson = result.optJSONObject(index);
						String steps = "第" + (index + 1) + newsJson.getString("steps");
						songPlayInfo = new SongPlayInfo();
						songPlayInfo.setPlayUrl(steps);
						playList.add(songPlayInfo);
					}
					getPlayController().playURLList(semanticResult.getUUID(), playList);
					getPlayController().playText(semanticResult.getUUID(), playList.get(0).getPlayUrl(), true, "", null);
					sendAiuiData("菜谱", "cookbook", result.toString());
				} else {
					getPlayController().playURLList(semanticResult.getUUID(), null);
					getPlayController().playText(semanticResult.getUUID(), "没查询到相应结果", true, "", null);
				}
			}
			} catch(JSONException e){
				e.printStackTrace();
			}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.COOKBOOK);
	}
}
